/*
 * @Author: Wilbur
 * @Date: 2019-08-20 09:16:20
 * @Last Modified by: Wilbur
 * @Last Modified time: 2019-08-24 17:14:15
 */

<template>
    <div class="container recommend-container">
        <div class="operation-wrapper">
            <el-button
                type="primary"
                @click="fnAddClick"
                size="small">新增推荐商家</el-button>
        </div>
        <div class="content-wrapper">
            <div class="table-wrapper">
                <el-table
                    :data="tableData"
                    height="100%"
                    style="width: 100%;">
                    <el-table-column
                        header-align="left"
                        align="center"
                        :width="50"
                        label="序号">
                        <template slot-scope="scope">
                            <div class="table-index">
                                {{ scope.$index + 1 }}
                            </div>
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="shop_logo"
                        show-overflow-tooltip
                        label="店铺logo">
                        <template slot-scope="scope">
                            <img
                                class="sub-logo"
                                :src="scope.row.shop_logo"
                                alt="">
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="shop_title"
                        :formatter="emptyFormatter"
                        show-overflow-tooltip
                        width="150px"
                        label="店铺名称"/>
                    <el-table-column
                        prop="shop_info"
                        :formatter="emptyFormatter"
                        show-overflow-tooltip
                        label="店铺介绍"/>
                    <el-table-column
                        prop="recommend_reason"
                        :formatter="emptyFormatter"
                        show-overflow-tooltip
                        label="推荐理由"/>
                    <el-table-column
                        prop="recommend_goods_num"
                        :formatter="emptyFormatter"
                        show-overflow-tooltip
                        width="110px"
                        label="精选商品数量">
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                @click="handleRoute('GoodsList', {shop_title: scope.row.shop_title, is_chosen: 1})"
                                v-if="scope.row.recommend_goods_num >= 2">{{ scope.row.recommend_goods_num }}</el-button>
                            <el-tooltip
                                class="item"
                                effect="dark"
                                content="小于二件商品，考虑到样式问题，在移动端不展示"
                                placement="bottom">
                                <el-button
                                    type="text"
                                    style="color: red;"
                                    @click="handleRoute('GoodsList', {shop_title: scope.row.shop_title, is_chosen: 1})"
                                    v-if="scope.row.recommend_goods_num < 2">{{ scope.row.recommend_goods_num }}</el-button>
                            </el-tooltip>
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="type_status"
                        show-overflow-tooltip
                        label="店铺类别">
                        <template slot-scope="scope">
                            <template v-if="scope.row.shop_type == 1">自营</template>
                            <template v-if="scope.row.shop_type == 2">淘宝</template>
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="recommend_sort"
                        label="排序"
                    >
                        <template slot-scope="scope">
                            <div class="sort-wrap">
                                {{ scope.row.recommend_sort }}
                                <span class="sort-spliter" />
                                <el-button
                                    type="text"
                                    class="btn-sort"
                                    style="color: #606266"
                                    @click="fnEditSort(scope.row)"
                                >
                                    <i class="el-icon-edit" />
                                </el-button>
                            </div>
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="shelf_time"
                        :width="150"
                        label="上架时间"
                    >
                        <template slot-scope="scope">
                            {{ scope.row.shelf_time*1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="expire_time"
                        :width="150"
                        label="下架时间"
                    >
                        <template slot-scope="scope">
                            {{ scope.row.expire_time*1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="recommend_status"
                        show-overflow-tooltip
                        width="150px;"
                        label="状态">
                        <template slot-scope="scope">
                            <template v-if="scope.row.recommend_status == 1">已上架</template>
                            <template v-if="scope.row.recommend_status == 0">已下架</template>
                            <el-switch
                                :active-value="1"
                                :inactive-value="0"
                                style="margin-left: 10px;"
                                @change="fnChangeStatus(scope.row)"
                                v-model="scope.row.recommend_status"/>

                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="date"
                        label="操作"
                        :width="250"
                    >
                        <template slot-scope="scope">
                            <div>
                                <el-button
                                    type="text"
                                    @click="handleRoute('GoodsList', {shop_title: scope.row.shop_title})">设置精选商品</el-button>
                                <el-button
                                    type="text"
                                    @click="fnEditRecommend(scope.row)">编辑</el-button>
                                <el-button
                                    type="text"
                                    style="margin-left:10px;"
                                    @click="fnDelMaterial(scope.row)">
                                    删除
                                </el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <!-- <div class="pagination-wrapper clearfix">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData" />
            </div> -->
        </div>
        <!-- 添加推荐商家弹窗 -->
        <el-dialog
            title="推荐商家"
            width="575px"
            :visible.sync="dialogAddVisible">
            <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleForm"
                label-width="80px"
                class="demo-ruleForm">
                <el-form-item
                    label="店铺名称"
                    prop="shop_no">
                    <el-select
                        v-model="ruleForm.shop_no"
                        filterable
                        remote
                        :disabled="chooseDisable"
                        placeholder="输入手机号/店铺名称"
                        :remote-method="fnSearchShop"
                        @change="handleSelect"
                        :loading="loading">
                        <el-option
                            v-for="item in shopListChoose"
                            :key="item.shop_no"
                            :label="item.shop_title"
                            :value="item.shop_no"/>
                    </el-select>
                </el-form-item>

                <el-form-item
                    v-if="ruleForm.shop_logo"
                    label="店铺Logo"
                    prop="shop_logo">

                    <img
                        :src="ruleForm.shop_logo"
                        class="avatar">
                </el-form-item>

                <el-form-item
                    label="店铺介绍"
                    prop="shop_info">
                    <el-input
                        v-model="ruleForm.shop_info"
                        placeholder="15字以内"
                        class="add-dialog-input"/>
                </el-form-item>

                <el-form-item
                    label="排序号"
                    prop="recommend_sort">
                    <el-input
                        v-model="ruleForm.recommend_sort"
                        type="number"
                        class="add-dialog-input"/>
                </el-form-item>

                <el-form-item
                    label="上架周期"
                    required>
                    <el-col :span="11">
                        <el-form-item prop="shelf_time">
                            <el-date-picker
                                type="datetime"
                                placeholder="开始日期"
                                v-model="ruleForm.shelf_time"
                                style="width: 100%;"/>
                        </el-form-item>
                    </el-col>
                    <el-col
                        class="line"
                        style="text-align: center;"
                        :span="2">-</el-col>
                    <el-col :span="11">
                        <el-form-item prop="expire_time">
                            <el-date-picker
                                type="datetime"
                                placeholder="结束日期"
                                v-model="ruleForm.expire_time"
                                style="width: 100%;"/>
                        </el-form-item>
                    </el-col>
                </el-form-item>
                <el-form-item
                    label="推荐理由"
                    prop="recommend_reason">
                    <el-input
                        type="textarea"
                        placeholder="20字以内"
                        v-model="ruleForm.recommend_reason"/>
                </el-form-item>
            </el-form>
            <div
                slot="footer"
                class="dialog-footer">
                <el-button @click="dialogAddVisible = false">取 消</el-button>
                <el-button
                    type="primary"
                    @click="submitForm('ruleForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        var checkTypeLogo = (rule, value, callback) => {
            if (this.ruleForm.shop_logo == '') {
                return callback(new Error('请上传分类图片'));
            }else{
                callback();
            }
        };

        return {
            authResult: {},
            tableData: [],
            filterData: {
                parent_type_no: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            baseUrl: '',
            mobileUrl: '',
            escape: escape,
            sessionTicket: localStorage.getItem('sessionTicket'),
            dialogAddVisible: false,
            typeList: [],
            selectList: [],
            ruleForm: {
                shop_info: '',
                recommend_reason: '',
                shelf_time: '',
                expire_time: '',
                recommend_sort: '',
            },
            typeDisabled: false,
            rules: {
                shop_info: [
                    { required: true, message: '请输入店铺介绍', trigger: 'blur' },
                    { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
                ],
                shop_no: [
                    { required: true, message: '请选择推荐店铺', trigger: 'change' }
                ],
                recommend_sort: [
                    { required: true, message: '请输入排序号', trigger: 'blur' }
                ],
                recommend_reason: [
                    { required: true, message: '请输入推荐理由', trigger: 'blur' },
                    { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
                ],
                shelf_time: [
                    { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
                ],
                expire_time: [
                    { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
                ]
            },
            shopListChoose: [],
            loading: false,
            chooseDisable: false
        };
    },
    created() {
        this.authResult = this.$route.meta.authResult;
        this.baseUrl = this.$baseUrl;
        this.mobileUrl = location.host == 'phaadmin.wxhand.com' ? 'http://class.zsdx.cn' : 'http://task.wxhand.com';
        this.$store.commit('SET_HEADER', [
            {
                title: "校园代理",
            },
            {
                title: '商城'
            },
            {
                title: '推荐商家'
            }
        ]);
        this.getTableData();
    },
    mounted() {
    },
    methods: {
        fnEditSort(item) {
            this.$prompt('修改排序', '', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^(0|\+?[1-9][0-9]*)$/,
                inputPlaceholder: '请输入序号',
                inputErrorMessage: '请输入正整数',
                closeOnClickModal: false,
            }).then(({ value }) => {

                this.$post('/student/shop_recommend%5Cmodify', {
                    shop_no: item.shop_no,
                    recommend_sort: value
                }, resp => {
                    if (resp.code == 1) {
                        this.$set(item, 'recommend_sort', value);
                        this.$message({
                            type: 'success',
                            message: '操作成功',
                            center: true
                        });
                    }else{
                        this.$message({
                            type: 'warning',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            }).catch(() => {
            });
        },
        fnChangeStatus(item) {
            let status = item.recommend_status;

            this.$post('/student/shop_recommend%5Cmodify', {
                shop_no: item.shop_no,
                recommend_status: item.recommend_status
            }, resp => {
                if (resp.code == 1) {
                    this.$message({
                        type: 'success',
                        message: '操作成功',
                        center: true
                    });

                }else{
                    this.$message({
                        type: 'warning',
                        message: resp.msg,
                        center: true
                    });
                    this.$set(item, 'recommend_status', status);
                }
            });
        },
        fnSearchShop(word) {
            if(word != '') {
                this.loading = true;
                this.$post('/student/shop%5Csuggest', {
                    keyword: word
                }, resp => {
                    if (resp.code == 1) {
                        this.shopListChoose = resp.data.list;
                    }
                });
                this.loading = false;
            }else{
                this.shopListChoose = [];
            }
        },
        handleSelect(no) {
            this.shopListChoose.map((item, index) => {
                if(item.shop_no == no) {
                    this.ruleForm.shop_logo = item.shop_logo;
                }
            });
        },
        fnAddClick() {
            this.ruleForm = {
                shop_no: '',
                shop_info: '',
                recommend_reason: '',
                shelf_time: '',
                expire_time: '',
                recommend_sort: '',
            };
            this.chooseDisable = false;
            this.dialogAddVisible = true;
        },
        fnEditRecommend(row) {
            this.$post('/student/shop_recommend%5Cget', {
                shop_no: row.shop_no
            }, resp => {
                if (resp.code == 1) {
                    this.ruleForm.shop_title = resp.data.shop_title;
                    this.ruleForm.expire_time = new Date(resp.data.expire_time * 1000);
                    this.ruleForm.shop_info = resp.data.shop_info;
                    this.ruleForm.recommend_reason = resp.data.recommend_reason;
                    this.ruleForm.shelf_time = new Date(resp.data.shelf_time * 1000);
                    this.ruleForm.recommend_sort = resp.data.recommend_sort;
                    this.ruleForm.shop_logo = resp.data.shop_logo;
                    this.shopListChoose = [{
                        shop_no: resp.data.shop_no,
                        shop_title: resp.data.shop_title,
                        shop_logo: resp.data.shop_logo
                    }];
                    // this.fnSearchShop(resp.data.shop_title);
                    this.ruleForm.shop_no = resp.data.shop_no;
                    this.chooseDisable = true;
                    this.dialogAddVisible = true;
                }
            });
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$post('/student/shop_recommend%5Csave', {
                        shop_no: this.ruleForm.shop_no,
                        shop_info: this.ruleForm.shop_info,
                        recommend_reason: this.ruleForm.recommend_reason,
                        shelf_time: this.ruleForm.shelf_time.getTime() / 1000,
                        expire_time: this.ruleForm.expire_time.getTime() / 1000,
                        recommend_sort: this.ruleForm.recommend_sort,
                    }, resp => {
                        if (resp.code == 1) {
                            this.dialogAddVisible = false;
                            this.getTableData(this.page.now_page);
                            this.$message({
                                type: 'success',
                                message: '操作成功',
                                center: true
                            });
                        }
                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        fnDelMaterial(row) {
            this.$confirm('删除该推荐商家, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/shop_recommend%5Cdelete', {
                    shop_no: row.shop_no
                }, resp => {
                    if (resp.code == 1) {
                        this.getTableData(this.page.now_page);
                        this.$message({
                            type: 'success',
                            message: '操作成功',
                            center: true
                        });
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        /**
         * 获取表格数据
         */
        getTableData(page) {
            this.$post('/student/shop%5Cget_recommend_list', {
                page: page || this.page.now_page,
                keyword: ''
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 跳转页面
         */
        handleRoute(name, query) {
            let obj = {
                name
            };

            if (query) {
                obj.query = query;
            }

            this.$router.push(obj);
        },

        /**
         * 列表渲染辅助函数：列表项为空处理
         */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
.avatar{
    width: 100px;
    height: 100px;
    border-radius: 4px;
}
.container {
    display: flex;
    flex-direction: column;
    width: 100%;
}
.add-dialog-input{
    width: 350px;
}
.operation-wrapper {
    display: flex;
    padding: 12px 23px;
    background-color: #fff;
    .select-container{
        flex: 1;
    }
    .el-button {
        float: right;
        margin-left: 10px;
    }
}
.content-wrapper {
    flex: 1;
    display: flex;
    flex-direction: column;
    margin: 15px;
    overflow: hidden;
}
.search-wrapper {
    padding: 15px;
    background-color: #fff;
}
.filter-item {
    .el-form-item {
        margin-bottom: 0;
        white-space: nowrap;
    }
    .el-input {
        width: auto;
        vertical-align: middle;
    }
    .el-button {
        margin-left: 15px;
        vertical-align: middle;
    }
}
.table-wrapper {
    flex: 1;
    background-color: #fff;
    .sub-logo{
        width: 50px;
        height: 50px;
    }
}
.pagination-wrapper {
    padding-top: 10px;
    .el-pagination {
        float: right;
        padding: 0;
        background-color: transparent;
    }
}
</style>
<style lang="less">
.recommend-container {
    tbody {
        .cell {
            display: flex;
            align-items: center;
            height: 48px;
        }
    }
    .upload-good-image{
        border: 1px solid red;
        width: 100px;
        height: 100px;
        display: flex;
        border: 1px dashed #d9d9d9;
        border-radius: 4px;
        align-items: center;
        justify-content: center;
        font-size: 34px;
        color: #8c939d;
        img{
            width: 100px;
            height: 100px;
        }
    }
}
</style>
